package com.yunkeji.api.user.controller;

import com.yunkeji.api.user.bo.RealAuthBo;
import com.yunkeji.api.user.service.AppUserAuthService;
import com.yunkeji.api.user.vo.AuthQuery;
import com.yunkeji.api.user.vo.InvitedParamVo;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.enums.app.AccountTypeEnums;
import com.yunkeji.config.annotation.ApiAuth;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 用户相关接口-权限
 *
 * @author 老张
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/user")
public class AppUserAuthController extends BaseController {

    private final AppUserAuthService appUserAuthService;
    /**
     * 注销账号
     *
     * @author 老张
     */
    @PostMapping("/delAccount")
    public R<Void> toLogOff() {
        appUserAuthService.delAccount();
        return R.ok();
    }
    /**
     * 退出登录
     *
     * @author 老张
     */
    @PostMapping("/logout")
    @ApiAuth(allow = {AccountTypeEnums.客服, AccountTypeEnums.用户})
    public R<Void> logout() {
        appUserAuthService.logout();
        return R.ok();
    }
    /**
     * 获取最新的kmsgToken
     *
     * @author 老张
     */
    @PostMapping("/refreshKmsgToken")
    @ApiAuth(allow = {AccountTypeEnums.客服, AccountTypeEnums.用户})
    public R refreshKmsgToken() {
        String token = appUserAuthService.refreshKmsgToken();
        return R.ok("OK", token);
    }

    /**
     * 提交实名认证/提交真人认证
     *
     * @author 老张
     */
    @PostMapping("/realAuth")
    @RepeatSubmit(interval = 3, timeUnit = TimeUnit.SECONDS, message = "操作过快,稍后再试")
    public R<Void> realAuth(@RequestBody RealAuthBo bo) {
        appUserAuthService.realAuth(bo);
        return R.ok();
    }

    /**
     * 认证状态查询
     *
     * @author 老张
     */
    @PostMapping("/authQuery")
    public R<AuthQuery> authQuery() {
        AuthQuery authQuery = appUserAuthService.authQuery();
        return R.ok(authQuery);
    }

    /**
     * 认证参数查询-邀请好友是否满足数量
     *
     * @author 老张
     */
    @PostMapping("/authParam")
    public R<InvitedParamVo> authParam() {
        InvitedParamVo authQuery = appUserAuthService.getInvitedParam();
        return R.ok(authQuery);
    }
}
